#include <stdio.h>
#include "TextBox.h"

TextBox::TextBox(TextureManager * textureManager, char * text,
				 Vector2D position, Vector2D size, 
				 float fontSize)
:MenuElement(textureManager, "", position, size, NULL)
{

	numTextLines = 0;
	textLines = NULL;
	this->textSize = fontSize;
	int charPerLine = (this->size.x)/ fontSize;
	int textLen = strlen(text);
	bool done = false;
	int beginWordIndex = 0;
	int beginLine = 0;
	Vector2D textLinePosition = position;
	
	while(beginWordIndex < textLen)
	{
	
		int bufferIndex = 0;
		int beginWordThisLineIndex = 0;
		
		char * buffer = (char *)malloc(sizeof(char) * (charPerLine + 1));
		printf("made a buffer, textLen = %d, beginWordIndex = %d\n", textLen, beginWordIndex);
		for(int i=0; i < charPerLine && (bufferIndex + i) < textLen; i++)
		{
			printf("i = %d, charPerLine = %d, processing char *%c*\n", i, charPerLine, text[beginLine + i]);
			if(text[beginLine + i] == ' ' || text[beginLine + i] == '\t' || text[beginLine + i] == '\n' || text[beginLine + i] == '\0'||(beginLine + i) == textLen - 1)
			{
				memcpy(buffer + bufferIndex * sizeof(char), text + beginWordIndex *sizeof(char), sizeof(char) * (i-beginWordThisLineIndex + 1));
				bufferIndex += (i-beginWordThisLineIndex + 1);
				beginWordIndex = beginLine + i + 1;
				beginWordThisLineIndex = i + 1;
				if(text[beginLine + i] == '\n' || text[beginLine + i] == '\0')
					break;
			}
		
		}
		printf("beginWordIndex = %d\n", beginWordIndex);
		beginLine = beginWordIndex;
		if(bufferIndex > 0)
		{
			buffer[bufferIndex] = '\0';
			printf("buffer = ***%s***\n", buffer);
			numTextLines++;
			
			textLines = (PrintString **)realloc(textLines, sizeof(PrintString *) * numTextLines);
			textLines[numTextLines-1] = new PrintString(textureManager, buffer, textLinePosition, fontSize);
			textLinePosition.y += fontSize * 1.5;
		}
	}
	//this->size.y = GetNeededHeight();	
}

TextBox::~TextBox()
{
	if(textLines)
	{
		delete(textLines);
		textLines = NULL;
	}
}
char * TextBox::GetText()
{
	printf("TextBox::GetText()\n");
	char * text = NULL;
	for(int i=0; i < numTextLines; i++)
	{
		char * line = textLines[i]->GetString();
		if(text)
		{
			text = (char*)realloc(text, sizeof(char) * (strlen(text) + strlen(line)+1));
			strcpy(text + strlen(text), line);
		}
		else
		{
			text = (char*)malloc(sizeof(char) * (strlen(line)+1));
			strcpy(text, line);
		}
		
	}
	printf("TextBox::GetText() = %s\n", text);
	return text;
}
void TextBox::SetText(char * text)
{
	printf("TextBox::SetText = %s\n",text);
	if(textLines)
	{
		delete(textLines);
		textLines = NULL;
	}
	
	numTextLines = 0;
	int charPerLine = (this->size.x)/ textSize;
	int textLen = strlen(text);
	bool done = false;
	int beginWordIndex = 0;
	int beginLine = 0;
	Vector2D textLinePosition = position;
	
	while(beginWordIndex < textLen)
	{
	
		int bufferIndex = 0;
		int beginWordThisLineIndex = 0;
		
		char * buffer = (char *)malloc(sizeof(char) * (charPerLine + 1));
		printf("made a buffer, textLen = %d, beginWordIndex = %d\n", textLen, beginWordIndex);
		for(int i=0; i < charPerLine && (bufferIndex + i) < textLen; i++)
		{
			printf("i = %d, charPerLine = %d, processing char *%c*\n", i, charPerLine, text[beginLine + i]);
			if(((textLinePosition.y - position.y) >= size.y)||text[beginLine + i] == ' ' || text[beginLine + i] == '\t' || text[beginLine + i] == '\n' || text[beginLine + i] == '\0'||(beginLine + i) == textLen - 1||(i >= (charPerLine-1)))
			{
				memcpy(buffer + bufferIndex * sizeof(char), text + beginWordIndex *sizeof(char), sizeof(char) * (i-beginWordThisLineIndex + 1));
				bufferIndex += (i-beginWordThisLineIndex + 1);
				beginWordIndex = beginLine + i + 1;
				beginWordThisLineIndex = i + 1;
				if(text[beginLine + i] == '\n' || text[beginLine + i] == '\0')
					break;
			}
		
		}
		printf("beginWordIndex = %d\n", beginWordIndex);
		beginLine = beginWordIndex;
		printf("current size = %f\n", textLinePosition.y - position.y);
		printf("set size = %f\n", size.y);
		if(bufferIndex > 0)
		{
			buffer[bufferIndex] = '\0';
			printf("buffer = ***%s***\n", buffer);
			numTextLines++;
			
			textLines = (PrintString **)realloc(textLines, sizeof(PrintString *) * numTextLines);
			textLines[numTextLines-1] = new PrintString(textureManager, buffer, textLinePosition, textSize);
			textLinePosition.y += textSize * 1.5;
		}
	}
	//this->size.y = GetNeededHeight();	
	printf("TextBox::SetText Done\n");
}
void TextBox::Draw(Vector2D parentPosition)
{
	if(numTextLines <= 0)
		return;
		
	for(int i = 0; i < numTextLines; i++)
	{
		textLines[i]->Draw(parentPosition);
	}
	
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	glOrtho(0,100,100,0, -1, 1);
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();
	glTranslatef(parentPosition.x+position.x, parentPosition.y+position.y, 0);

	glColor3f(1,1,1);
	glDisable(GL_TEXTURE_2D);
	glBegin(GL_QUADS);
	glVertex2f(0,0);
	glVertex2f(size.x, 0);
	glVertex2f(size.x, size.y);
	glVertex2f(0, size.y);
	
	glEnd();
	
	glDisable(GL_TEXTURE_2D);
	glDisable(GL_BLEND);
	glPopMatrix();
	glDisable(GL_TEXTURE_2D);
	glDisable(GL_BLEND);
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
}
float TextBox::GetNeededHeight()
{
	return numTextLines * textSize * 1.5;
}
void TextBox::SetPosition(Vector2D position)
{
	this->position = position;
}

